﻿@model Bricks.Site.Models.Document.DocumentView

@{
    ViewBag.Title = "附件操作";
    Layout = "~/Views/Shared/_UserPage.cshtml";
}

@*引入uEditor*@
<script type="text/javascript" charset="utf-8" src="/Res/ueditor/ueditor.config.js"></script>
<script type="text/javascript" charset="utf-8" src="/Res/ueditor/ueditor.all.js"> </script>
<script type="text/javascript" charset="utf-8" src="/Res/ueditor/lang/zh-cn/zh-cn.js"></script>
<link rel="stylesheet" type="text/css" href="/Res/ueditor/themes/default/css/ueditor.css" />

<script src="@Url.Content("~/Res/scripts/json.js")"></script>

<!--添加ueditor隐藏（调用ueditor的上传功能）-->
<script type="text/plain" id="uploadEditor">
</script>
<script type="text/javascript">
    var _myEditor = UE.getEditor('uploadEditor');
    _myEditor.ready(function () {
        //设置编辑器不可用
        _myEditor.setDisabled();
        //隐藏编辑器，因为不会用到这个编辑器实例，所以要隐藏
        _myEditor.hide();
        ////侦听图片上传
        //_myEditor.addListener('beforeInsertImage', function (t, arg) {
        //    //将地址赋值给相应的input,只去第一张图片的路径
        //    $("#picture").attr("value", arg[0].src);
        //    //图片预览
        //    $("#preview").attr("src", arg[0].src);
        //})
        //侦听文件上传，取上传文件列表中第一个上传的文件的路径
        _myEditor.addListener('afterUpfile', function (t, arg) {
            //$("#file").attr("value", _myEditor.options.filePath + arg[0].url);
            //$("#file").attr("value", "upload ok");
            //$.messager.alert("提示", "upload ok", "info");
            //var fileJson = JSON.stringify(arg);
            //$("#file").attr("value", fileJson);
            //$("#hUploadInfo").attr("value", fileJson);

            //var myDate = new Date();
            for (var i = 0; i < arg.length; i++) {
                if (arg[i].uploadUser == "") {
                    arg[i].uploadUser = $("#hCurrentUser").val();
                }
                $('#documentGrid').datagrid('appendRow', {
                    FileName: arg[i].original,
                    FilePath: arg[i].url,
                    FileSize: arg[i].fileSize,
                    UploadUser: arg[i].uploadUser,
                    UploadTime: arg[i].uploadTime
                });
            }
        })
    });
</script>

@*实现对EasyUI的DataGird控件操作的JS代码*@
<script type="text/javascript">
    $(function () {
        var instanceID = $("#hInstanceID").val();
        var tag = $("#hTag").val();
        initTable(instanceID, tag);
    });

    function initTable(instanceID, tag) {
        $('#documentGrid').datagrid({
            //url: '/Document/GetDocumentList?instanceID=681764fd-aef8-4dcf-84bd-a2d700ffc9a0&Tag=Test',  //开发期测试
            url: '/Document/GetDocumentList?instanceID=' + instanceID + '&Tag=' + tag,  //运行期使用
            nowrap: true,
            autoRowHeight: false,
            striped: true,
            collapsible: true,
            rownumbers: true,
            remoteSort: false,
            idField: 'ID',
            checkOnSelect: true,
            selectOnCheck: true,
            singleSelect: true,
            //queryParams: queryData,
            columns: [[
                {
                    title: '@Html.LabelFor(m => m.FileName)', field: 'FileName', width: 270,
                    formatter: function (value, row, index) {
                        //target = "_blank"可避免乱码问题
                        //return '<a target="_blank" href="' + row["FilePath"] + '">' + value + '</a> ';
                        return '<a target="_blank" href="/Document/Download?filePath=' + row.FilePath + '&fileName=' + row.FileName + '">' + row.FileName + '</a>';
                    }
                },
                { title: '@Html.LabelFor(m => m.FileSize)', field: 'FileSize', width: 90, sortable: true },
                { field: 'UploadUserID', hidden: true },
                { title: '@Html.LabelFor(m => m.UploadUser)', field: 'UploadUser', width: 90, sortable: true },
                { title: '@Html.LabelFor(m => m.UploadTime)', field: 'UploadTime', width: 130, sortable: true }

            ]],
            toolbar: '#documentToolBar'
        });
    }

    var Document = {
        //上传
        Upload: function () {
            var myFiles = _myEditor.getDialog("attachment");
            myFiles.open();
        },

        //删除
        Delete: function () {
            var row = $("#documentGrid").datagrid("getSelected");
            if (row) {
                //$.messager.confirm('操作提示', "确认删除选中项吗？", function (r) {
                //    if (r) {
                //        $.post("/Document/Delete?ID=" + row.ID, function (result) {
                //            if (result.ResultType == 0) {  //0表示成功
                //                //移除删除的数据
                //                $.EasyuiSlide(result.Message);
                //                Document.Reload();
                //            }
                //            else {
                //                $.EasyuiMsg(result.Message);
                //            }
                //        });
                //    }
                //});
                $("#documentGrid").datagrid("deleteRow", $("#documentGrid").datagrid("getRowIndex", row));
            }
            else {
                $.EasyuiSlide("请选择一条记录");
            }
        },

        //下载
        Download: function () {
            $.EasyuiSlide("下载");
        },

        //重新加载数据
        Reload: function () {
            $("#documentGrid").datagrid("reload");
        }
    }
</script>

<!--面板（从数据库取得菜单名称和图标等）-->
<div class="easyui-panel" title="附件列表" fit="true">
    <form id="doc-model" method="post">
        <table id="documentGrid" style="height: 250px;"></table>
        <div id="documentToolBar">
            <a href='javascript:void(0)' class='easyui-linkbutton' onclick='Document.Upload()' data-options='plain:true,iconCls:"icon-application_add"' disabled="@ViewBag.BtnDisabled">上传</a>
            <a href='javascript:void(0)' class='easyui-linkbutton' onclick='Document.Download()' data-options='plain:true,iconCls:"icon-bank"' disabled="@ViewBag.BtnDisabled">下载</a>
            <a href='javascript:void(0)' class='easyui-linkbutton' onclick='Document.Delete()' data-options='plain:true,iconCls:"icon-bell"' disabled="@ViewBag.BtnDisabled">删除</a>
            <a href='javascript:void(0)' class='easyui-linkbutton' onclick='Document.Reload()' data-options='plain:true,iconCls:"icon-blue_lego"' disabled="@ViewBag.BtnDisabled">重置</a>
        </div>
    </form>
</div>
<input type="hidden" id="hInstanceID" value="@ViewBag.InstanceID" />
<input type="hidden" id="hTag" value="@ViewBag.Tag" />
<input type="hidden" id="hCurrentUser" value="@ViewBag.CurrentUser" />
